package com.ctshk.rpc.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.cms.dto.UserFeedbackDTO;
import com.ctshk.rpc.cms.entity.UserFeedback;
import com.ctshk.rpc.cms.mapper.UserFeedbackMapper;
import com.ctshk.rpc.cms.req.UserFeedbackAddReq;
import com.ctshk.rpc.cms.req.UserFeedbackQueryReq;
import com.ctshk.rpc.cms.service.IUserFeedbackService;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户反馈表 服务实现类
 * </p>
 *
 * @author LiZhiming
 * @since 2021-04-23
 */
@DubboService
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> implements IUserFeedbackService {

    @Override
    public Result insert(UserFeedbackAddReq req, TokenUser tu) {
        UserFeedback uf = EntityUtil.copy(req, UserFeedback.class);
        LocalDateTime now = LocalDateTime.now();
        Long id = IdWorker.getId();
        uf.setCreateId(tu.getId());
        uf.setGmtCreate(now);
        uf.setId(id);
        uf.setIsDeleted(IsDeletedCode.NO.getCode());
        int insert = baseMapper.insert(uf);
        if (insert > 0) {
            return Result.success(id);
        }
        return Result.failed(SystemError.CMS_23000);
    }

    @Override
    public Result<UserFeedbackDTO> queryById(String userFeedbackId) {
        UserFeedbackDTO b = null;
        QueryWrapper<UserFeedback> qw = new QueryWrapper<>();
        qw.lambda().eq(UserFeedback::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(UserFeedback::getId, userFeedbackId);
        UserFeedback uf = baseMapper.selectOne(qw);
        if (uf != null) {
            b = EntityUtil.copy(uf, UserFeedbackDTO.class);
        }
        return Result.success(b);
    }

    @Override
    public PageResponse<UserFeedbackDTO> queryList(UserFeedbackQueryReq req) {
        Page<UserFeedback> page = new Page(req.getPageNo(), req.getPageSize());
        QueryWrapper<UserFeedback> qw = new QueryWrapper();
        qw.lambda().eq(UserFeedback::getIsDeleted, IsDeletedCode.NO.getCode())
                .eq(req.getFeedbackTypeId() != null, UserFeedback::getFeedbackTypeId, req.getFeedbackTypeId())
                .eq(req.getFeedbackTypeName() != null, UserFeedback::getFeedbackTypeName, req.getFeedbackTypeName())
                .eq(req.getFeedbackChannels() != null, UserFeedback::getFeedbackChannels, req.getFeedbackChannels())
                .ge(req.getBeginTime() != null, UserFeedback::getGmtCreate, req.getBeginTime())
                .le(req.getEndTime() != null, UserFeedback::getGmtCreate, req.getEndTime());
        Page<UserFeedback> ufPage = baseMapper.selectPage(page, qw);
        if (CollectionUtils.isNotEmpty(ufPage.getRecords())) {
            List<UserFeedbackDTO> ufDTOS = EntityUtil.copyList(ufPage.getRecords(), UserFeedbackDTO.class);
            return new PageResponse<>(ufDTOS,
                    page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                    page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
        }
        return new PageResponse<>(null,
                page.getCurrent() == 1, page.getTotal() <= (page.getCurrent() *
                page.getSize()), page.getTotal(), page.getCurrent(), page.getSize());
    }
}
